package com.zengguang.testkaoshi.controller;

import com.zengguang.testkaoshi.pojo.Auth;
import com.zengguang.testkaoshi.services.impl.AuthService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "auth")
@RestController
@CrossOrigin
public class AuthAction {
    @Autowired
    private AuthService authService;
    @ApiOperation(value = "权限添加方法")
    @RequestMapping(value = "/addAuth",method = RequestMethod.GET)
    public ResponseMessage addAuth(Auth auth) {
        int isAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (auth != null) {
            //添加角色数据
            isAdd = authService.insertAuth(auth);
            if (isAdd == 1) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        }
        return Result.error("添加失败");
    }

    @ApiOperation(value = "权限表修改方法")
    @RequestMapping(value = "/updateAuth",method = RequestMethod.GET)
    public ResponseMessage updateAuth(Auth auth) {
        int isUpadte = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (auth != null) {
            //添加试卷数据
            isUpadte = authService.updateAuth(auth);
            if (isUpadte >0) {
                return Result.success("权限表信息更新成功");
            } else {
                return Result.error("权限表信息更新失败");
            }

        }
        return Result.error("权限表信息更新失败");
    }

    @ApiOperation(value = "权限表信息查询方法")
    @RequestMapping(value = "/selectAllauth",method = RequestMethod.GET)
    public ResponseMessage selectAllauth(Auth auth) {
        Map<String, List> map=new HashMap<>();
        List<Auth> list=authService.selectAllAuth();
        if (!list.isEmpty()){
            map.put("list",list);
            return Result.success("200",map);
        }
        return Result.error("查询失败");
    }

    @ApiOperation("删除权限表信息")
    @RequestMapping(value = "/deleteteAuth",method = RequestMethod.GET)
    public ResponseMessage deleteteAuth(int authid){
        int temp=0;
        if (authid!=0){
            temp=authService.deleteAuth(authid);
            if (temp>0) {
                return Result.success("删除权限表信息成功",temp);
            }
            else {
                return Result.error("删除权限表信息失败");
            }
        }

        return Result.error("权限表信息编号不为0");
    }
}
